import { app } from 'electron';
import * as path from 'path';
import { join } from 'path';
import fs from 'fs';

export const getConfigFn = () => {
  const filePath = join(app.getPath('userData'), '/Config/data.txt');

  const directoryPath = path.dirname(filePath);

  let defaultData = {
    host: 'localhost',
    syncHostSqlite: false,
  };

  if (!fs.existsSync(directoryPath)) {
    fs.mkdirSync(directoryPath, { recursive: true });
  }

  if (fs.existsSync(filePath)) {
    const fileData = fs.readFileSync(filePath, 'utf-8');

    defaultData = JSON.parse(fileData);
  } else {
    fs.writeFileSync(filePath, JSON.stringify(defaultData));
  }

  // const { syncHostSqlite } = defaultData;

  // console.log(syncHostSqlite);

  // if (syncHostSqlite) {
  //   console.log('同步数据库');
  // }

  return defaultData;
};

export const setConfigFn = (data) => {
  const filePath = join(app.getPath('userData'), '/Config/data.txt');

  const directoryPath = path.dirname(filePath);

  if (!fs.existsSync(directoryPath)) {
    fs.mkdirSync(directoryPath, { recursive: true });
  }

  fs.writeFileSync(filePath, JSON.stringify(data));
};
